package com.zdft.promotion.asyn.util;


import com.zdft.promotion.asyn.bean.AsynPushBean;
import com.zdft.promotion.asyn.model.AsynPushFiterModel;
import com.zdft.promotion.common.service.BusinessException;

import java.util.Date;

public class AsynDealUtil {

    /**
     * 处理成功返回
     */
    public final static int SUCCESS_RET = 1;
    /**
     * 处理失败返回
     */
    public final static int FAIL_RET = 0;

    /**
     * 默认状态
     */
    public final static int ZERO_INT = 0;

    /**
     * 默认状态
     */
    public final static int DEFAULT_STATUS = 0;

    /**
     * 处理成功状态
     */
    public final static int SUCCESS_STATUS = 1;

    /**
     * 处理失败状态
     */
    public final static int FAIL_STATUS = 2;

    /**
     * 操作类型 1：新增
     */
    public final static int OPERATE_ADD = 1;

    /**
     * 操作类型 2：修改
     */
    public final static int OPERATE_UPDATE = 2;

    /**
     * 操作类型 3：删除
     */
    public final static int OPERATE_DELETE = 3;

    public static AsynPushFiterModel bulidPushFilterModel(int type, String subType, int operateType, int fetchNum,
                                                          int failCount) {
        AsynPushFiterModel model = new AsynPushFiterModel();
        model.setType(type);
        model.setSubType(subType);
        model.setOperateType(operateType);
        model.setFailCount(failCount);
        model.setPageSize(fetchNum);
        return model;
    }

    /**
     * 组装
     *
     * @param refrenceId  插入主表主键
     * @param type        模块类型 1：客户 2： 商品 3：供应商 4：生产厂家
     * @param subType     具体处理方法名
     * @param operateType 操作类型
     * @param createUser  操作人
     * @return
     */
    public static AsynPushBean bulidAsynPushBean(String refrenceId, int type, String subType, int operateType,
                                                 String createUser, String remark) {
        AsynPushBean bean = new AsynPushBean();
        bean.setRefrenceId(refrenceId);
        bean.setType(type);
        bean.setSubType(subType);
        bean.setOperateType(operateType);
        bean.setStatus(DEFAULT_STATUS);
        bean.setFailCount(ZERO_INT);
        bean.setCreateUser(createUser);
        bean.setUpdateUser(createUser);
        Date now = new Date();
        bean.setCreateTime(now);
        bean.setUpdateTime(now);
        bean.setRemark(remark);
        return bean;
    }

	/*public static AsynPushFiterModel pushFilterModel(int type, String subType, int fetchNum, int failCount) {
		AsynPushFiterModel model = new AsynPushFiterModel();
		model.setType(type);
		model.setSubType(subType);
		model.setFailCount(failCount);
		model.setIslimitPageSize(false);
		model.setPageSize(fetchNum);
		return model;
	}*/

    /**
     * 组装处理结果
     * @param reslut
     * @param bean
     * @param ex
     */
    public static void bulidDealResult(int reslut, AsynPushBean bean, Exception ex) {
        bean.setStatus(SUCCESS_STATUS);
        // 若处理失败，则设置状态为处理失败，处理次数+1
        if (FAIL_RET == reslut) {
            bean.setStatus(FAIL_STATUS);
            bean.setFailCount(bean.getFailCount() + 1);
        }
        if (ex != null) {
            if(ex instanceof BusinessException){
                ex = (BusinessException) ex;
                String message = ex.getMessage();
                        message = message.length() > 300
                        ? message.substring(0, 300) : message;
                bean.setRemark(message);
            } else if (ex.getMessage() != null) {
                String message = ex.getStackTrace()[0].getClassName() + ex.getStackTrace()[0].getMethodName() + ":"
                        + ex.getMessage();
                message = message.length() > 400
                        ? message.substring(0, 400) : message;
                bean.setRemark(message);
            }
        }
    }

    /**
     * 组装处理结果
     * @param reslut
     * @param bean
     * @param remark
     */
    public static void bulidDealResult(int reslut,  String remark, AsynPushBean bean) {
        bean.setStatus(SUCCESS_STATUS);
        // 若处理失败，则设置状态为处理失败，处理次数+1
        if (FAIL_RET == reslut) {
            bean.setStatus(FAIL_STATUS);
            bean.setFailCount(bean.getFailCount() + 1);
        }
        bean.setRemark(remark);
    }

    public static boolean getExceptionPushBean(AsynPushBean pushBean, Exception ex) {
        boolean requeue = false;
        pushBean.setFailCount(pushBean.getFailCount() == null ? 1 : pushBean.getFailCount() + 1);
        if (ex.getMessage() != null && ex.getMessage().length() >= 500) {
            pushBean.setRemark(ex.getMessage().substring(0, 499));
        } else {
            pushBean.setRemark(ex.getMessage());
        }

        if (pushBean.getFailCount() <= 10) {
            requeue = true;
        } else {
            pushBean.setStatus(2);
        }
        return requeue;
    }
}
